package com.jh.calendar;

import java.util.Calendar;

public class CalendarAPI 
{
	
		
	public void calPrint(int year,int month)
	{		
		Calendar cal = Calendar.getInstance();
		cal.set(year, month-1, 1);//현재의 요일을 가져와서 특정위치부터 찍어내기 위해서
		
		int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);//set되어있는 요일을 가져옴
		int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH); //set 되어진 값에서 월의 일수를 가져옴
	
		System.out.printf("\t\t %d년 \t %d월\n ",year,month);
		System.out.println("일\t월\t화\t수\t목\t금\t토\t");
		
		cal.set(year, month-2, 1);
		int lastDay2 = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
		// 전달의 요일까지 공백을 찍음
		for (int i = lastDay2+2-dayOfWeek; i <= lastDay2; i++) 
		{
			System.out.print(i+"\t");
		}
		
		for (int i = 1; i <= lastDay; i++) 
		{
			System.out.print(i+"\t");
			if ((i+dayOfWeek-1)%7==0) 
			{
				System.out.println();
			}
		}
		
		for (int i = 1; i <= dayOfEnd(dayOfWeek,lastDay); i++) 
		{
			System.out.print(i+"\t");
		}

		System.out.println();
		System.out.println("========================================================");
	}

	private int dayOfEnd(int dayOfWeek, int lastDay) 
	{
		int dayOfEnd = 7-((dayOfWeek-1+lastDay)%7);
		return dayOfEnd==7?0:dayOfEnd;
	}
	
	
	
}
